import Vue from 'vue'
import notifyComponent from './notify.vue'

// const NotifyConstructor = Vue.extend(require('./notify.vue'))
const NotifyConstructor = Vue.extend(notifyComponent)

const instance = new NotifyConstructor({
  el: document.createElement('div')
})
console.log(instance)

let timer = null
let lock = false

NotifyConstructor.prototype.closeNotify = function () {
  instance.classes = 'notify-out'

  setTimeout(() => {
    const el = instance.$el
    el.parentNode && el.parentNode.removeChild(el)
    lock = false
  }, 150)

  typeof this.callback === 'function' && this.callback()
}

const Notify = (options = {}) => {
  if (lock && (!options.mes || options.mes === instance.mes)) {
    return
  }
  timer && clearTimeout(timer)
  instance.classes = ''
  instance.mes = options.mes
  instance.timeout = ~~options.timeout || 3000
  instance.callback = options.callback
  lock = true

  document.body.appendChild(instance.$el)

  setTimeout(function () {
    instance.classes = 'init'
  }, 10)

  instance.$el.addEventListener('click', () => {
    clearTimeout(timer)
    instance.closeNotify()
  })

  timer = setTimeout(() => {
    clearTimeout(timer)
    instance.closeNotify()
  }, instance.timeout)
}

export default Notify
